package org.chengn.level_middle.context1901;

/**
 * @Title: Context1901
 * @Author ChengN
 * @Package org.chengn.level_middle.context1901
 * @Date 2023/12/20 0:00
 * @description: 寻找峰值Ⅱ
 */
public class Context1901 {
    public int[] findPeakGrid(int[][] mat) {
        int m = mat.length, n = mat[0].length;
        int l = 0, r = m - 1;
        while (l <= r) {
            int mid = (l + r) >> 1;
            int idx = -1, maxValue = -1;
            for (int i = 0; i < n; ++i) {
                if (mat[mid][i] > maxValue) {
                    maxValue = mat[mid][i];
                    idx = i;
                }
            }
            if (mid > 0 && mat[mid][idx] < mat[mid - 1][idx]) {
                r = mid - 1;
                continue;
            }
            if (mid < m - 1 && mat[mid][idx] < mat[mid + 1][idx]) {
                l = mid + 1;
                continue;
            }
            return new int[]{mid, idx};
        }
        return new int[0];
    }
}
